<?header('Content-Type:text/html;charset=utf-8');?>
<?include ($_SERVER ["DOCUMENT_ROOT"] . "/lib/module/conn_db.php");?>
<?include ($_SERVER ["DOCUMENT_ROOT"] . "/lib/module/Util.php");?>
<?php //DB Open

	$result = null;
	$db = new MyDB ( false );
	$db->err_report = true;

	$init[0] = "";
    $init[1] = "and ( category_name rlike '^(ㄱ)' or (category_name >= '가'  and category_name < '나') )";
	$init[2] = "and ( category_name rlike '^(ㄴ)' or (category_name >= '나'  and category_name < '다') )";
	$init[3] = "and ( category_name rlike '^(ㄷ)' or (category_name >= '다'  and category_name < '라') )";
	$init[4] = "and ( category_name rlike '^(ㄹ)' or (category_name >= '라'  and category_name < '마') )";
	$init[5] = "and ( category_name rlike '^(ㅁ)' or (category_name >= '마'  and category_name < '바') )";
	$init[6] = "and ( category_name rlike '^(ㅂ)' or (category_name >= '바'  and category_name < '사') )";
	$init[7] = "and ( category_name rlike '^(ㅅ)' or (category_name >= '사'  and category_name < '아') )";
	$init[8] = "and ( category_name rlike '^(ㅇ)' or (category_name >= '아'  and category_name < '자') )";
	$init[9] = "and ( category_name rlike '^(ㅈ)' or (category_name >= '자'  and category_name < '차') )";
	$init[10] = "and ( category_name rlike '^(ㅊ)' or (category_name >= '차'  and category_name < '카') )";
	$init[11] = "and ( category_name rlike '^(ㅋ)' or (category_name >= '카'  and category_name < '타') )";
	$init[12] = "and ( category_name rlike '^(ㅌ)' or (category_name >= '타'  and category_name < '파') )";
	$init[13] = "and ( category_name rlike '^(ㅍ)' or (category_name >= '파'  and category_name < '하') )";
	$init[14] = "and ( category_name rlike '^(ㅎ)' or (category_name >= '하' ))";

?>

<?php // 초성 -> 성씨 검색

	$lastName = $_POST['last_name'];

	$initial = $_POST['initial'];
	$id = "last_name";

	$where = "where category_parent=1 ".$init[$initial]." order by category_name asc";

	$sql = " SELECT count(*) as total FROM tb_category ".$where;
	$db->query ( $sql );
	$root_total = mysql_result ( $db->result, 0, "total" );
	if ($root_total > 0) {
		$sql = " SELECT category_lft, category_rgt,category_hanja, category_name FROM tb_category ".$where;
		$db->query ( $sql );
		$root_query = $db->result;
	}

	$lastOpt="<option value=''>선택</option>";
	if($root_total!=0){
		while ($root_list = mysql_fetch_object($root_query)){
			$_aaa=$root_list->category_lft."|".$root_list->category_rgt==$lastName ? "selected":"";
			$lastOpt .= "<option value='".$root_list->category_lft."|".$root_list->category_rgt."' ".$_aaa.">".$root_list->category_name."(".$root_list->category_hanja.")</option>";
		}
	}
?>

<?php // 성씨 -> 본관 검색

	$last=explode("|",$lastName);

	$home = $_POST['home'];

	$sql = ' SELECT count(*) as total FROM tb_category WHERE category_lft BETWEEN '.($last[0]+1).' AND '.($last[1]-1);
	$db->query ( $sql );
	$sub_total = mysql_result ( $db->result, 0, "total" );
	if ($sub_total > 0) {
		$sql = ' SELECT category_lft, category_rgt, category_name, category_hanja, category_sid FROM tb_category  WHERE category_lft BETWEEN '.($last[0]+1).' AND '.($last[1]-1).' ORDER BY category_lft ASC;';
		$db->query ( $sql );
		$sub_query = $db->result;
	}

	$id = "home";

	$homeOpt="<option value=''>선택</option>";
	if($sub_total!=0){
		while ($sub_list = mysql_fetch_object($sub_query)){
			$_aa=$sub_list->category_sid==$home ? "selected":"";
			$homeOpt .= "<option value='".$sub_list->category_sid."' ".$_aa.">".$sub_list->category_name."(".$sub_list->category_hanja.")</option>";
		}
	}
?>

<?php //DB Close
	$db->close ();

?>


<SCRIPT LANGUAGE="JavaScript">
 <!--
   top.receive("<?=$lastOpt?>","<?=$homeOpt?>");
   //alert("<?=$id?>,<?=$CateOpt?>,<?=$CateSecond?>");
 //-->
 </SCRIPT>
